package com.ayh.campusfun.common.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author ayh
 * @create 2021/3/24 0024 11:52
 * redis配置序列化
 */

@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory connectionFactory){
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//        String 类型key序列化
        template.setKeySerializer(new StringRedisSerializer());
//        String 类型value序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//        Hash 类型key序列化
        template.setHashKeySerializer(new StringRedisSerializer());
//        Hash 类型value序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(connectionFactory);
        return template;
    }
}
